<?php
/**
 * Модель - выпуск журнала. Примечательно, что любая запись в платонике может иметь, а может не иметь поле выпуск журнала
 * Те получается любая запись в системе может быть опубликована в журнале, и соответственно этот класс 
 * содержит методы для управления этими связями и их отображения
 * 
 * @package default
 * @author Alexander Sibiryakov
 **/
class Model_Issue extends RL_Model2 {
	public function getIssueSetByItemOid( $item_oid ) {
		
		$sql = "SELECT 
		iss.issue_number AS issue_number,
		tdi.title AS tdi_title
		FROM store_class_allfeed afeed
		INNER JOIN store_class_issue iss ON iss.oid = afeed.issue_oid
		INNER JOIN store_class_tdi tdi ON iss.tdi_oid = tdi.oid
		WHERE afeed.oid = ".$item_oid;
		
		$rs = new DBStoreResult($this->store, $this->store->query( $sql ));
		if ($rs) {
			$item = $rs->next();
			$row = $rs->rs->current();	
			return array( "issue_number"=>$item->data['issue_number'], "tdi_title"=>$item->data['tdi_title'] );
		} else return false;	
	}
	
	public function getOneIssue( $issue_oid ) {
		$sql = "SELECT 
		iss.issue_number AS issue_number,
		iss.issue_name AS issue_name,
		iss.publish_date AS publish_date,
		iss.issue_descr AS issue_descr,
		tdi.title AS tdi_title,
		tdi.url AS tdi_url,
		tdi.oid AS tdi_oid,
		tdi.color AS tdi_color,		
		afeed.title AS item_title,
		afeed.oid AS item_oid
		FROM store_class_issue iss 
		LEFT OUTER JOIN store_class_allfeed afeed ON iss.oid = afeed.issue_oid
		LEFT OUTER JOIN store_class_tdi tdi ON tdi.oid = afeed.tdi_oid
		WHERE iss.oid = ".$issue_oid;

		$ors = new DBStoreResult($this->store, $this->store->query( $sql ));

		if (!$ors) { echo "there is no available feed for you"; return false; }
		
		while ($obj = $ors->next()) { 
			$row = $ors->rs->current();	
			$item["title"] = $row['item_title'];
			$item["item_oid"] = $row['item_oid'];
			if (is_numeric($row["item_oid"])) {
				$rubric[ $row["tdi_oid"] ]["items"][] = $item;
				$rubric[ $row["tdi_oid"] ]["tdi_title"] = $row['tdi_title'];
				$rubric[ $row["tdi_oid"] ]["tdi_color"] = $row['tdi_color'];			
				$rubric[ $row["tdi_oid"] ]["tdi_url"] = $row['tdi_url'];
			}
			unset($item);
		}
		
		
		$issue["issue_descr"] = $row["issue_descr"];
		$issue["issue_name"] = $row["issue_name"];		
		$issue["issue_number"] = $row["issue_number"];
		$issue["issue_oid"] = $issue_oid;		
		$issue["publish_date"] = RL_CyrDate::newFromStr( "%Y-%m-%d", $row['publish_date'] );
		return array("issue"=>$issue, "rubrics"=>$rubric);
		
	}
	
}